package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tools.Format;
import core.Torrent;

public class FenetreProperty extends JFrame{

    private JLabel titre;
    private JLabel description;
    private JLabel destination;
    private JLabel size;
    private JButton ok;
    private JPanel conteneur;
    private JLabel seeders;
    private JLabel leechers;

    
    public FenetreProperty(Torrent t){

        this.titre = new JLabel("Nom : "+t.getName());
        this.description = new JLabel("Commentaire : "+t.getComment());
        this.destination = new JLabel("Destination : ");
        this.size = new JLabel("Size : "+Format.bytes(t.getLength()));
        this.ok = new JButton("OK");
        this.conteneur = new JPanel();
        this.seeders = new JLabel("Seeders : "+getSeeders());
        this.leechers = new JLabel("Leechers : "+getLeechers());

        this.ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                FenetreProperty.this.dispose();
            }
        });

        this.ok.setPreferredSize(new Dimension(40, 20));
        this.conteneur.setLayout(new GridLayout(6, 1));
        this.conteneur.add(titre);
        this.conteneur.add(description);
        this.conteneur.add(destination);
        this.conteneur.add(size);
        this.conteneur.add(seeders);
        this.conteneur.add(leechers);
        

        this.add(this.conteneur, BorderLayout.CENTER);
        this.add(ok, BorderLayout.SOUTH);
        this.pack();
        this.setSize(200, 200);
        this.setVisible(true);

    }
    
    
    private int getSeeders(){
        
        int nbseeders=0;
        
        try {

            String hostname = "bttracker.debian.org";
            int port = 6969;
            InetAddress addr = InetAddress.getByName(hostname);
            Socket socket = new Socket(addr, port);

            String path = "/announce?info_hash=x%60%a0tkm%c4%d0%be%11%5b%c9%7d%bdA%3b%3d%27%84%01&peer_id=-TR2130-n12r35yy9y17&port=51413&uploaded=0&downloaded=0&left=679477248&numwant=80&key=duzwhmd8&compact=1&supportcrypto=0&event=started";
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            wr.write("GET " + path + " HTTP/1.1\r\n");
            wr.write("Connection: close\r\n");
            wr.write("\r\n");

            wr.flush();

            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            int line;
            
            while ((line = rd.read()) != -1) {
                if (line == 100)
                    if (rd.read() == 56)
                        break;
            }
            
            for (int i=0; i<10; i++){
                rd.read();
            }
            
            int p = 0;
            int n;
            ArrayList<Integer> nombre = new ArrayList<Integer>();
            while (( n = rd.read()) != 101){
                nombre.add(n-48);
                p++;
            }

            for (int j=0; j<p; j++){
                
                nbseeders+=nombre.get(j)*Math.pow(10,nombre.size()-1-j);

            }
            System.out.println(nbseeders+" seeders");
        
            wr.close();
            rd.close();
        } catch (Exception e) {
        }
        
        
        return nbseeders;
    }
    
    private int getLeechers(){
        
        int nbleechers=0;
        
        try {

            String hostname = "bttracker.debian.org";
            int port = 6969;
            InetAddress addr = InetAddress.getByName(hostname);
            Socket socket = new Socket(addr, port);

            String path = "/announce?info_hash=x%60%a0tkm%c4%d0%be%11%5b%c9%7d%bdA%3b%3d%27%84%01&peer_id=-TR2130-n12r35yy9y17&port=51413&uploaded=0&downloaded=0&left=679477248&numwant=80&key=duzwhmd8&compact=1&supportcrypto=0&event=started";
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            wr.write("GET " + path + " HTTP/1.1\r\n");
            wr.write("Connection: close\r\n");
            wr.write("\r\n");

            wr.flush();

            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            int line;

            while ((line = rd.read()) != -1) {
                if (line == 49)
                    if (rd.read() == 48)
                        if(rd.read() == 58)
                            break;
            }
            
            for (int i=0; i<11; i++){
                rd.read();
            }
            
            int p = 0;
            int n;
            ArrayList<Integer> nombre = new ArrayList<Integer>();
            while (( n = rd.read()) != 101){
                nombre.add(n-48);
                p++;
            }

            for (int j=0; j<p; j++){
                
                nbleechers+=nombre.get(j)*Math.pow(10,nombre.size()-1-j);

            }
            System.out.println(nbleechers+" leechers");
            
            wr.close();
            rd.close();
        } catch (Exception e) {
        }
        
        
        return nbleechers;
    }
}